﻿using Hydrogen.ULib.Attributes;
using Hydrogen.ULib.DataStructure;
using Hydrogen.ULib.Extends;
using UnityEngine;

namespace Hydrogen.MarkovChain.SolidMap2DScene
{
    public class SolidMap2DScene : MonoBehaviour
    {
        private SolidMap2dResManager _resMgr;
        private Map _map;
        private Driver _driver;

        [SerializeField]
        private string _ruleName = "MazeBacktracker";

        private void Awake()
        {
            this._map = GetComponent<Map>();
            this._driver = GetComponent<Driver>();
        }

        private void Start()
        {
            this._resMgr = new SolidMap2dResManager();
            this._map?.Init(this._resMgr);

        }

        private void OnDisable()
        {
            this._resMgr.Dispose();
        }

        private void Run()
        {
            if (!this._driver) return;
            if (this._driver.Status == DriveStatus.Running) return;

            this._driver?.Init(this._resMgr, this._map, this._ruleName);
            this._driver.StartRun();
        }

        #region 测试
#if UNITY_EDITOR
        [Space, Header("测试")]
        [Button("Run"), SerializeField]
        private bool _testRun;


        [Button("TestRotate90"), SerializeField]
        private bool _testRotate90;


        private void TestRotate90()
        {
            TwoDimensionDic<int, int, SolidType> origin = new TwoDimensionDic<int, int, SolidType>();
            //origin[0, 0] = SolidType.R;
            //origin[1, 0] = SolidType.G;
            //origin[2, 0] = SolidType.W;
            //origin[0, 1] = SolidType.U;
            //origin[1, 1] = SolidType.D;
            //origin[2, 1] = SolidType.O;
            origin[0, 0] = SolidType.U;
            origin[1, 0] = SolidType.G;
            origin[2, 0] = SolidType.G;

            this._map.ResetMap();
            origin.Foreach(this._map.SetColor);

            this.Delay(1, () =>
            {
                var rotated = SolidBlock.RotateBlock(origin, 3, 1, Direction.L);
                this._map.ResetMap();
                rotated.Foreach(this._map.SetColor);
            });
        }
#endif
        #endregion
    }
}